package cn.tedu.mall.seckill.timer.job;

import cn.tedu.mall.product.service.ISkuService;
import cn.tedu.mall.product.service.ISpuService;
import cn.tedu.mall.seckill.mapper.SeckillSpuMapper;
import cn.tedu.mall.seckill.utils.RedisBloomUtils;
import cn.tedu.mall.seckill.utils.SeckillCacheUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class SeckillBloomInitialJob implements Job {
    @Autowired
    private RedisBloomUtils redisBloomUtils;
    @Autowired
    private SeckillSpuMapper spuMapper;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //首先查询向批次的所有数据
        //有需求要求查询两个或更多批次的数据
        String bloomTodayKey = SeckillCacheUtils.getBloomFilterKey(LocalDate.now());
        //获取明天的key
        String bloomTomorrowKey = SeckillCacheUtils.getBloomFilterKey(LocalDate.now().plusDays(1));
        Long[] spuIds = spuMapper.findAllSeckillSpuIds();
        String[] spuIdsStr = new String[spuIds.length];
        for (int i = 0; i < spuIds.length; i++) {
            spuIdsStr[i]=spuIds[i]+"";
        }
        //最终布隆过滤器中包含这两个批次的spuId,防止缓存穿透
        redisBloomUtils.bfmadd(bloomTodayKey,spuIdsStr);
        redisBloomUtils.bfmadd(bloomTomorrowKey,spuIdsStr);
        System.out.println("两个布隆过滤器加载完毕!");
    }












}
